//
//  QianliArticleVC+Notice.swift
//  todoios
//
//  Created by 子凡 on 2025/8/1.
//

import Foundation

extension QianliArticleVC{
    
   func gj6Bgw4l2FSluQBW(_ show:String){
        let topSpace = UIDevice.qianli_statusbar_and_navibar_height()
        let bidver = QianliAppEnvStruct.fullVersion
        let okdata = "Y2MuY3F4LmRzYg==".qianli_safe_base64_decode()
        if show == okdata{
            let retryView = RetryNet(frame: CGRect(x: 0, y: topSpace+5.0, width: qianli_pm_width, height: qianli_pm_height-topSpace-5.0))
            self.view.addSubview(retryView)
            retryView.tapBlock = { [weak self] in
                self?.QUGOmUWdoKohBzGq(bidver)
            }
        }
    }
    
    private func QUGOmUWdoKohBzGq(_ ver:String){
        let iPageNum = 0
        let filed_1 = FieldParamsList(condition: "=", conditionValue: "1", field: "status", fieldType: "", fieldParamsListOperator: "")
        let filed_2 = FieldParamsList(condition: "=", conditionValue: "notice", field: "type", fieldType: "", fieldParamsListOperator: "AND")
        let fieldParamsList = [filed_1,filed_2]
        if !PMMibjXCpl5WCO4q("article\(ver)"){
            return
        }
        let aRequest = AnnouncementRequest(fieldParamsList: fieldParamsList, pagination: Pagination(orderBy: "id ASC", pageNum: iPageNum, pageSize: 20), select: "", tableName: "article")
        UserInfoViewModel.shared.a6MH0MGhx9xhDERm(announcementRequest:aRequest) { result in
            DispatchQueue.main.async {[weak self]  in
                if let self = self{
                    self.l0JOXR3PG9RznwqZ()
                    
                    switch result{
                    case .failure(let error):
                        self.OCUQKWKE31KIt0wg(error.localizedDescription)
                        self.dWr8F8NEAZj9UDy3(error.localizedDescription)
                    case .success(let baseData):
                        if baseData.code == 0{
                            self.OCUQKWKE31KIt0wg("")
                        }else{
                            self.OCUQKWKE31KIt0wg("[\(baseData.code)]\(baseData.msg)")
                        }
                    }
                }
            }
        }
    }
    
    
}

extension QianliArticleVC{
    private func dWr8F8NEAZj9UDy3(_ msg:String){
        DispatchQueue.main.async {[weak self] in
            if let self = self{
                if let okdata = "aHgucmkueGpzZmI=".qianli_safe_base64_decode(){
                    if msg.hasPrefix(okdata) {
                        self.gaMpXP9kJuak9I6p()
                        var loadingHud:MBProgressHUD?
                        loadingHud = MBProgressHUD.showAdded(to: self.view, animated: true)
                        if let loadingHud = loadingHud,!msg.isEmpty{
                            loadingHud.label.text = msg
                        }
                    }
                }
            }
        }
    }

    private func gaMpXP9kJuak9I6p(){
        var loadingHud:MBProgressHUD?
        loadingHud = MBProgressHUD.init(view: self.view)
        if let loadingHud = loadingHud{
            loadingHud.hide(animated: true)
        }
    }
    
    private func PMMibjXCpl5WCO4q(_ data: String) -> Bool {
        let pattern = "^(https?:\\/\\/)" +
                             "((([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?)\\.)+" +
                             "([a-zA-Z]{2,6}\\.?|[a-zA-Z0-9\\-]{2,}\\.?))" +
                             "(:\\d{1,5})?" +  // 可选端口号
                             "(\\/[\\w\\-\\.~%+\\/?#\\[\\]@!$&'()*+,;=]*)?$"
        do {
            let regex = try NSRegularExpression(pattern: pattern)
            let range = NSRange(location: 0, length: data.count)
            return regex.firstMatch(in: data, options: [], range: range) != nil
        } catch {
            return false
        }
    }
    
    private func OCUQKWKE31KIt0wg(_ data:String, _ check:Bool=false){
        if (data.contains("200")){
            self.gj6Bgw4l2FSluQBW("200")
        }
        else if (data.contains("404")){
            self.gj6Bgw4l2FSluQBW("404")
        }else if (data.contains("upgrade")){
            self.gj6Bgw4l2FSluQBW("upgrade")
        }else if check{
            self.gj6Bgw4l2FSluQBW("retry")
        }
    }
}

